package Chat;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.io.IOException;
import java.io.ObjectOutputStream;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.JButton;
import javax.swing.JTextField;

import gui.ServerInfo;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ChatForClient extends JFrame {

	public ServerInfo si;
	public ObjectOutputStream oos;
	
	private JPanel contentPane;
	private JTextField txtInputChat;
	public JTextPane txtShowChat;
	
	public Integer paraIndex;
	public DefaultStyledDocument doc;

	public ChatForClient(ServerInfo s) {
		setTitle("Chat From Client"+ s.chatSocket.getInetAddress().getHostAddress());
		si = s;
		
		try {
			oos = new ObjectOutputStream(si.chatSocket.getOutputStream());
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setSize(450, 300);
		contentPane = new JPanel();
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 11, 414, 178);
		contentPane.add(scrollPane);
		
		doc = new DefaultStyledDocument();
		paraIndex = 0;
		
		txtShowChat = new JTextPane(doc);
		txtShowChat.setEditable(false);
		scrollPane.setViewportView(txtShowChat);
		
		JPanel panel = new JPanel();
		panel.setBounds(10, 200, 414, 51);
		contentPane.add(panel);
		panel.setLayout(null);
		
		txtInputChat = new JTextField();
		txtInputChat.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent arg0) {
				if(arg0.getKeyCode() == KeyEvent.VK_ENTER)
					sendChat();
			}
		});
		txtInputChat.setBounds(10, 11, 330, 29);
		panel.add(txtInputChat);
		
		JButton btnSend = new JButton("G\u1EEDi");
		btnSend.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				sendChat();
			}
		});
		btnSend.setBounds(349, 14, 55, 23);
		panel.add(btnSend);
	}
	
	private void sendChat() {
		
		String s = txtInputChat.getText();
		s = s.trim();
		if(!s.isEmpty() && s != null) {
			//String s1 = si.chatSocket.getLocalAddress().getHostName() + ": " + s + "\n";
			String s1 = si.chatSocket.getLocalAddress().getHostName() + ": " + s + "\n";
			try {
				synchronized(txtShowChat) {
					synchronized (paraIndex) {
						doc.insertString(paraIndex, s1, null);
						paraIndex += s1.length();
					}
				}
				oos.writeUTF(s);
				oos.flush();
				oos.reset();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (BadLocationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		txtInputChat.setText(null);
	}
}
